package fab.forms.fields 
{
	import fab.forms.error.FormError;
	import fab.forms.fields.Element;
	import fl.controls.RadioButton;
	import fl.controls.RadioButtonGroup;
	/**
	 * ...
	 * @author fab
	 */
	public class RadioButtonGroupElement extends Element
	{
		static public var liste:Vector.<RadioButtonGroupElement> = new Vector.<RadioButtonGroupElement>();
		public var group:RadioButtonGroup;
		public function RadioButtonGroupElement(rb:RadioButton ) 
		{
			group = rb.group;
			name = group.name;
			liste.push( this );
		}
		override public function validate():FormError
		{
			var error:FormError;
			if ( hasError( FormError.RB_UNCHECKED ) && !selected() )
			{
				error = getError( FormError.RB_UNCHECKED );
			}
			return error;			
		}		
		private function selected():Boolean
		{
			return group.selection != null;			
		}	
		override public function getValue():String
		{
			return group.selection.value as String;
		}	
		static public function getByGroup( rb:RadioButton ) : RadioButtonGroupElement
		{
			for each ( var rbge:RadioButtonGroupElement in liste ) if ( rbge.group == rb.group ) return rbge;
			return new RadioButtonGroupElement( rb );
		}		
	}
}